home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / misc.c < prev    next >
C/C++ Source or Header  |  1991-10-03  |  4KB  |  248 lines

  1. /* @(#) $Header: misc.c,v 1.10 91/10/03 11:05:13 deyke Exp $ */
  2.  
  3. /* Miscellaneous machine independent utilities
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include "global.h"
  9. #include "socket.h"
  10. #include "mbuf.h"
  11.  
  12. /* Select from an array of strings, or return ascii number if out of range */
  13. char *
  14. smsg(msgs,nmsgs,n)
  15. char *msgs[];
  16. unsigned nmsgs,n;
  17. {
  18.     static char buf[16];
  19.  
  20.     if(n < nmsgs && msgs[n] != NULLCHAR)
  21.         return msgs[n];
  22.     sprintf(buf,"%u",n);
  23.     return buf;
  24. }
  25.  
  26. /* Convert hex-ascii to integer */
  27. int
  28. htoi(s)
  29. char *s;
  30. {
  31.     int i = 0;
  32.     char c;
  33.  
  34.     while((c = *s++) != '\0'){
  35.         if(c == 'x')
  36.             continue;       /* allow 0x notation */
  37.         if('0' <= c && c <= '9')
  38.             i = (i * 16) + (c - '0');
  39.         else if('a' <= c && c <= 'f')
  40.             i = (i * 16) + (c - 'a' + 10);
  41.         else if('A' <= c && c <= 'F')
  42.             i = (i * 16) + (c - 'A' + 10);
  43.         else
  44.             break;
  45.     }
  46.     return i;
  47. }
  48. /* replace terminating end of line marker(s) with null */
  49. void
  50. rip(s)
  51. register char *s;
  52. {
  53.     register char *cp = s;
  54.  
  55.     while (*cp)
  56.         cp++;
  57.     while (--cp >= s && (*cp == '\r' || *cp == '\n'))
  58.         ;
  59.     cp[1] = '\0';
  60. }
  61.  
  62. #if !(defined(hpux)||defined(__hpux))
  63. /* Copy a string to a malloc'ed buffer. Turbo C has this one in its
  64.  * library, but it doesn't call mallocw() and can therefore return NULL.
  65.  * NOS uses of strdup() generally don't check for NULL, so they need this one.
  66.  */
  67. char *
  68. strdup(s)
  69. const char *s;
  70. {
  71.     register char *out;
  72.     register int len;
  73.  
  74.     if(s == NULLCHAR)
  75.         return NULLCHAR;
  76.     len = strlen(s);
  77.     out = mallocw(len+1);
  78.     /* This is probably a tad faster than strcpy, since we know the len */
  79.     memcpy(out,s,len);
  80.     out[len] = '\0';
  81.     return out;
  82. }
  83. #endif
  84. /* Routines not needed for Turbo 2.0, but available for older libraries */
  85. /* #ifdef  AZTEC */
  86.  
  87. /* Case-insensitive string comparison */
  88.  
  89. int stricmp(s1, s2)
  90. char *s1, *s2;
  91. {
  92.   while (tolower(uchar(*s1)) == tolower(uchar(*s2++)))
  93.     if (!*s1++) return 0;
  94.   return tolower(uchar(*s1)) - tolower(uchar(s2[-1]));
  95. }
  96.  
  97. strnicmp(a,b,n)
  98. char *a,*b;
  99. size_t n;
  100. {
  101.     char a1,b1;
  102.  
  103.     while(n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0'){
  104.         if(a1 == b1)
  105.             continue;       /* No need to convert */
  106.         a1 = tolower(a1);
  107.         b1 = tolower(b1);
  108.         if(a1 == b1)
  109.             continue;       /* NOW they match! */
  110.         if(a1 > b1)
  111.             return 1;
  112.         if(a1 < b1)
  113.             return -1;
  114.     }
  115.     return 0;
  116. }
  117.  
  118. #ifdef  AZTEC
  119. char *
  120. strtok(s1,s2)
  121. char *s1;       /* Source string (first call) or NULL */
  122. #ifdef  __STDC__        /* Ugly kludge for aztec's declaration */
  123. const char *s2; /* Delimiter string */
  124. #else
  125. char *s2;       /* Delimiter string */
  126. #endif
  127. {
  128.     static int isdelim();
  129.     static char *next;
  130.     register char *cp;
  131.     char *tmp;
  132.  
  133.     if(s2 == NULLCHAR)
  134.         return NULLCHAR;        /* Must give delimiter string */
  135.  
  136.     if(s1 != NULLCHAR)
  137.         next = s1;              /* First call */
  138.  
  139.     if(next == NULLCHAR)
  140.         return NULLCHAR;        /* No more */
  141.  
  142.     /* Find beginning of this token */
  143.     for(cp = next;*cp != '\0' && isdelim(*cp,s2);cp++)
  144.         ;
  145.  
  146.     if(*cp == '\0')
  147.         return NULLCHAR;        /* Trailing delimiters, no token */
  148.  
  149.     /* Save the beginning of this token, and find its end */
  150.     tmp = cp;
  151.     next = NULLCHAR;        /* In case we don't find another delim */
  152.     for(;*cp != '\0';cp++){
  153.         if(isdelim(*cp,s2)){
  154.             *cp = '\0';
  155.             next = cp + 1;  /* Next call will begin here */
  156.             break;
  157.         }
  158.     }
  159.     return tmp;
  160. }
  161. static int
  162. isdelim(c,delim)
  163. char c;
  164. register char *delim;
  165. {
  166.     char d;
  167.  
  168.     while((d = *delim++) != '\0'){
  169.         if(c == d)
  170.             return 1;
  171.     }
  172.     return 0;
  173. }
  174. #endif  /* AZTEC */
  175.  
  176. /* Host-network conversion routines, replaced on the x86 with
  177.  * assembler code in pcgen.asm
  178.  */
  179. #ifndef MSDOS
  180. /* Put a long in host order into a char array in network order */
  181. char *
  182. put32(cp,x)
  183. register char *cp;
  184. int32 x;
  185. {
  186.     *cp++ = x >> 24;
  187.     *cp++ = x >> 16;
  188.     *cp++ = x >> 8;
  189.     *cp++ = x;
  190.     return cp;
  191. }
  192. /* Put a short in host order into a char array in network order */
  193. char *
  194. put16(cp,x)
  195. register char *cp;
  196. int16 x;
  197. {
  198.     *cp++ = x >> 8;
  199.     *cp++ = x;
  200.  
  201.     return cp;
  202. }
  203. int16
  204. get16(cp)
  205. register char *cp;
  206. {
  207.     register int16 x;
  208.  
  209.     x = uchar(*cp++);
  210.     x <<= 8;
  211.     x |= uchar(*cp);
  212.     return x;
  213. }
  214. /* Machine-independent, alignment insensitive network-to-host long conversion */
  215. int32
  216. get32(cp)
  217. register char *cp;
  218. {
  219.     int32 rval;
  220.  
  221.     rval = uchar(*cp++);
  222.     rval <<= 8;
  223.     rval |= uchar(*cp++);
  224.     rval <<= 8;
  225.     rval |= uchar(*cp++);
  226.     rval <<= 8;
  227.     rval |= uchar(*cp);
  228.  
  229.     return rval;
  230. }
  231. /* Compute int(log2(x)) */
  232. int
  233. log2(x)
  234. register int16 x;
  235. {
  236.     register int n = 16;
  237.     for(;n != 0;n--){
  238.         if(x & 0x8000)
  239.             break;
  240.         x <<= 1;
  241.     }
  242.     n--;
  243.     return n;
  244. }
  245.  
  246. #endif
  247.  
  248.